package com.example.appmanager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.appmanager.Appconfig.Appconfig;
import com.example.appmanager.ui.home.HomeFragment;
import com.example.appmanager.user.Login;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
    private AppBarConfiguration mAppBarConfiguration;
    //    private FloatingActionButton fab;
    private DrawerLayout drawer;
    private NavigationView navigationView;
    private NavController navController;
    private Toolbar toolbar;
    private String uName;
    private String uPwd;
    private TextView userRealName;//用户的真实姓名
    private TextView userAccount;//用户的账户号
    private ImageView imageView;//用户的头像
    private ConstraintLayout constraintLayout;
    private Fragment fragment;

    /*这里应该有一个异步请求网络的数据 获取用户的基本信息*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intiUi();///初始化页面的控件
        getData();/*给页面赋值*/
        getOnClickListener();//绑定监听的事件
    }

    @Override
    protected void onResume() {

        /*这里表示在跳转的过程中*/
        //int id = (int) getIntent().getExtras().get("userLoginInfo");
        super.onResume();
    }

    /*页面的控件进行赋值操作*/
    private void getData() {
        setSupportActionBar(toolbar);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        // int i1 = R.id.nav_home;
        // int i2 = R.id.nav_gallery;
        // int i3 = R.id.nav_slideshow;
        int[] show = { R.id.nav_gallery, R.id.nav_slideshow/*,R.id.action_settings*/};
        mAppBarConfiguration = new AppBarConfiguration.Builder(/*这里是把几个其他页面装入里面*/
                show)
                .setDrawerLayout(drawer)
                .build();
        navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
        //设置用户的头部基本信息
        userAccount.setText(uName);
        userRealName.setText(uPwd);
        Glide.with(getApplicationContext()).load(Appconfig.BASE_IMAG + getIntent().getStringExtra("image")).into(imageView);
    }

//    onPrepareOptionsMenu

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }

    /*监听Android中的menu里面的控件*/
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        //监听Men你里面的控件的事件
        Log.d("MainActivity", String.valueOf(item.getGroupId()));
        switch (item.getItemId()) {
            case R.id.action_settings:
                Log.d("MainActivity111", "这个setting已经被监听！！");
                Intent view = new Intent(getApplicationContext(), Login.class);
                startActivity(view);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /*初始化界面的控件和变量的值
     * */
    public void intiUi() {
//        fab = findViewById(R.id.fab);
        drawer = findViewById(R.id.drawer_layout);///绑定activity_main.xml页面！
        navigationView = findViewById(R.id.nav_view);// /绑定activity_main.xml页面的NavigationView控件！
        //绑定activity_man.xml里面的Toolbar控件
        toolbar = findViewById(R.id.toolbar);
        userRealName = navigationView.getHeaderView(0).findViewById(R.id.user_real_name);//用户的真实姓名
        userAccount = navigationView.getHeaderView(0).findViewById(R.id.user_account);//用户的账户号
        imageView = navigationView.getHeaderView(0).findViewById(R.id.user_imageView);//用户的头像
        constraintLayout = findViewById(R.id.constraintLayout_main)/*.findViewById(R.id.nav_host_fragment)*/;
        /*
        bundle.putString("uName",uName);
        bundle.putString("uPwd",userPwd);
        * */
        uName = getIntent().getStringExtra("uName");

        uPwd = getIntent().getStringExtra("uRName");
//        bundle.putString("userEntity",userEntity.toString());
        String userEntity = getIntent().getStringExtra("userEntity");

        if (userEntity!=null){
            Log.d("MainActivity", uName + "----->" + uPwd);
            Log.d("MainActivity", userEntity );
        }

    }


    /*页面的监听方法！*/
    private void getOnClickListener() {

        navigationView.getMenu().findItem(R.id.nav_home02).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Bundle bundle = new Bundle();
                bundle.putString("uName",getIntent().getStringExtra("uName"));
                bundle.putString("uRName", getIntent().getStringExtra("uRName"));
                Intent intent = new Intent();
                intent.putExtras(bundle);
                intent.setClass(MainActivity.this, HomeFragment.class);
                startActivity(intent);
                return false;
            }
        });




    }
}
